DROP TRIGGER VasarloTorles;
DROP TRIGGER KepernyoTorles;
DROP TRIGGER VideokartyaTorles;

CREATE TRIGGER VasarloTorles
ON Vasarlo
INSTEAD OF DELETE
AS
	DELETE FROM Vasarlas WHERE Vasarlas.SzemSzam = (SELECT SzemSzam FROM deleted)
	DELETE FROM Vasarlo WHERE Vasarlo.SzemSzam = (SELECT SzemSzam FROM deleted)	
GO

/*
//////////////////////////////////////////////////
*/

CREATE TRIGGER KepernyoTorles
ON Kepernyok
INSTEAD OF DELETE
AS
	DECLARE @ID INT;
	DECLARE @TID INT;
	
	DELETE FROM Vasarlas WHERE TermekID IN (SELECT TermekID FROM Felepites WHERE AlkatreszID = (SELECT AlkatreszID FROM deleted))
	
	DELETE FROM Raktar WHERE TermekID IN (SELECT TermekID FROM Felepites WHERE AlkatreszID = (SELECT AlkatreszID FROM deleted) )
		
	CREATE TABLE #TempTable (Col1 INT)
	INSERT INTO #TempTable (Col1)
	SELECT TermekID FROM Felepites WHERE AlkatreszID = (SELECT AlkatreszID FROM deleted) 
			
	DELETE FROM Felepites WHERE TermekID IN (SELECT TermekID FROM Felepites WHERE AlkatreszID = (SELECT AlkatreszID FROM deleted) )
  	
	DELETE FROM Termekek WHERE TermekID IN (SELECT Col1 FROM #TempTable)
	
	DELETE FROM Kepernyok WHERE AlkatreszID = (SELECT AlkatreszID FROM deleted)
	
	DELETE FROM AlkTulajd WHERE	AlkatreszID = (SELECT AlkatreszID FROM deleted)
	
	DELETE FROM Alkatreszek WHERE AlkID = (SELECT AlkatreszID FROM deleted)
	
	DROP TABLE #TempTable;
GO

/*
SELECT * FROM Termekek

SELECT * FROM Kepernyok

SELECT * FROM Vasarlas

SELECT * FROM Felepites WHERE TermekID = 1

DELETE FROM Kepernyok WHERE AlkatreszID = 9;
*/

CREATE TRIGGER VideokartyaTorles
ON VideoKartyak
INSTEAD OF DELETE
AS
	DECLARE @ID INT;
	DECLARE @TID INT;
	
	DELETE FROM Vasarlas WHERE TermekID IN (SELECT TermekID FROM Felepites WHERE AlkatreszID = (SELECT AlkatreszID FROM deleted))
	
	DELETE FROM Raktar WHERE TermekID IN (SELECT TermekID FROM Felepites WHERE AlkatreszID = (SELECT AlkatreszID FROM deleted) )
		
	CREATE TABLE #TempTable (Col1 INT)
	INSERT INTO #TempTable (Col1)
	SELECT TermekID FROM Felepites WHERE AlkatreszID = (SELECT AlkatreszID FROM deleted) 
			
	DELETE FROM Felepites WHERE TermekID IN (SELECT TermekID FROM Felepites WHERE AlkatreszID = (SELECT AlkatreszID FROM deleted) )
  	
	DELETE FROM Termekek WHERE TermekID IN (SELECT Col1 FROM #TempTable)
	
	DELETE FROM Kepernyok WHERE AlkatreszID = (SELECT AlkatreszID FROM deleted)
	
	DELETE FROM AlkTulajd WHERE	AlkatreszID = (SELECT AlkatreszID FROM deleted)
	
	DELETE FROM Alkatreszek WHERE AlkID = (SELECT AlkatreszID FROM deleted)
	
	DROP TABLE #TempTable;
GO
